package ro.dandanciu.webgallery.domainmodel;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * @author Dan
 *
 */
@NamedQueries(
	{
		@NamedQuery(name = "Gallery.findRootByOwner", 
				query = "SELECT g FROM Gallery g WHERE g.name LIKE :filter and g.owner.userId LIKE :userId and g.parent is null"),
		@NamedQuery(name = "Gallery.findByOwnerAndParent", 
				query = "SELECT g FROM Gallery g WHERE g.name LIKE :filter and g.owner.userId LIKE :userId and  g.parent.id = :galleryId "),
		@NamedQuery(name = "Gallery.findAllPublic", 
				query = "SELECT g FROM Gallery g WHERE g.name LIKE :filter and g.publicAccess = true "),
		@NamedQuery(name = "Gallery.findByOwnerAndName", 
				query = "SELECT g FROM Gallery g WHERE g.owner.userId = :ownerUserId and g.name = :galleryName "),
	}
)
@Entity
@Table(name="TP_GALLERY")
public class Gallery {
	
	/**
	 * The identifier of the gallery.
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name="K_GALLERY", length=30)
	private long id;
	
	/**
	 * The name of the gallery.
	 */
	@Column(name="C_NAME", length=60) 
	private String name;
	
	/**
	 * A description of the gallery.
	 */
	@Column(name="C_DESCRIPTION", length=200) 
	private String description;
	
	/**
	 * Parent gallery, if null, then is root gallery.
	 */
	@ManyToOne
	@JoinColumn(name="F_PARENT")
	private Gallery parent;
	
	@ManyToOne
	@JoinColumn(name="F_USER")
	private User owner;
	
	@Column(name="B_PUBLIC")
	private boolean publicAccess;
	
	@OneToMany(targetEntity=Gallery.class,
            mappedBy="parent")
	private List<Gallery> subGalleries;

	/**
	 * The timestamp of the last time this item was updated.
	 * Mapped to D_UPDATED.
	 */
	@Column(name="D_UPDATED")
	@Temporal(value=TemporalType.TIMESTAMP)
	private Date updated;
	
	/**
	 * The user id/software which has performed the last update.
	 * Mapped to C_UPDATER.
	 */
	@Column(name="C_UPDATER", length=30) 
	private String updater;
	
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Gallery getParent() {
		return parent;
	}

	public void setParent(Gallery parent) {
		this.parent = parent;
	}

	public User getOwner() {
		return owner;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}

	public List<Gallery> getSubGalleries() {
		return subGalleries;
	}

	public void setSubGalleries(List<Gallery> subGalleries) {
		this.subGalleries = subGalleries;
	}

	public Date getUpdated() {
		return updated;
	}

	public void setUpdated(Date updated) {
		this.updated = updated;
	}

	public String getUpdater() {
		return updater;
	}

	public void setUpdater(String updater) {
		this.updater = updater;
	}

	public boolean isPublicAccess() {
		return publicAccess;
	}

	public void setPublicAccess(boolean publicAccess) {
		this.publicAccess = publicAccess;
	}

}
